package com.dc.chat.app.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dc.chat.app.service.FileService;
import com.dc.chat.app.service.mp.SensitiveService;
import com.dc.chat.entity.dao.Sensitive;
import com.dc.chat.entity.to.ResponseBean;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static org.springframework.util.DigestUtils.md5DigestAsHex;

@Service
public class FileServiceImpl implements FileService {
@Resource
    SensitiveService.SensitiveMapper sensitiveMapper;
    @Override
    public ResponseBean<String> upload(MultipartFile multipartFile) throws IOException {
        String md5 = md5DigestAsHex(multipartFile.getInputStream());
        if(sensitiveMapper.selectList(Wrappers.lambdaQuery(new Sensitive()).eq(Sensitive::getType,2).eq(Sensitive::getContent,md5)).size()!=0){
            return ResponseBean.failed("");
        }
        String originalFilename = multipartFile.getOriginalFilename();
        String[] split = originalFilename.split("\\.");
        String houzhui = split[split.length - 1];
        String qianzhui = UUID.randomUUID().toString();
        String fileName=qianzhui+"."+houzhui;
        multipartFile.transferTo(new File(""+fileName));
        return ResponseBean.success("上传成功","http://yungee.cn/wechat/file/"+fileName);
    }

    @Override
    public void downLoad(String fileName, HttpServletResponse resp) throws IOException {
        File file=new File("d:\\chat\\img\\"+fileName);
        FileInputStream fileInputStream=new FileInputStream(file);
        ServletOutputStream outputStream = resp.getOutputStream();
        byte[] buffer=new byte[100];
        int read = fileInputStream.read(buffer);
        while (read!=-1) {
            outputStream.write(buffer);
            read = fileInputStream.read(buffer);
        }
    }

    @Override
    public ResponseBean<String> fileupload(MultipartFile multipartFile) throws IOException {
        String originalFilename = multipartFile.getOriginalFilename();
        String[] split = originalFilename.split("\\.");
        String houzhui = split[split.length - 1];
        String qianzhui = UUID.randomUUID().toString();
        String fileName=qianzhui+"."+houzhui;
        multipartFile.transferTo(new File("d:\\chat\\file\\"+fileName));
        Map map=new HashMap();
        map.put("url","http://127.0.0.1:8080/file/"+fileName);
        map.put("fileName",originalFilename);
        map.put("fileSize",multipartFile.getSize());
        ObjectMapper objectMapper=new ObjectMapper();
        String s = objectMapper.writeValueAsString(map);
        return ResponseBean.success("上传成功",s);
    }
}
